package cn.iocoder.yudao.module.infrastructure.wechat.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infrastructure.wechat.convert.WechatTemplateConvert;
import cn.iocoder.yudao.module.infrastructure.wechat.pojo.po.WechatTemplate;
import cn.iocoder.yudao.module.infrastructure.wechat.pojo.vo.WechatTemplateCreateReqVO;
import cn.iocoder.yudao.module.infrastructure.wechat.pojo.vo.WechatTemplatePageReqVO;
import cn.iocoder.yudao.module.infrastructure.wechat.pojo.vo.WechatTemplateRespVO;
import cn.iocoder.yudao.module.infrastructure.wechat.pojo.vo.WechatTemplateUpdateReqVO;
import cn.iocoder.yudao.module.infrastructure.wechat.service.WechatTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 微信消息模板")
@RestController
@RequestMapping("/system/notify/wechat-template")
@Validated
public class WechatTemplateController {

    @Resource
    private WechatTemplateService wechatTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建微信消息模板")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Long> createWechatTemplate(@Valid @RequestBody WechatTemplateCreateReqVO createReqVO) {
        return success(wechatTemplateService.createWechatTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信消息模板")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> updateWechatTemplate(@Valid @RequestBody WechatTemplateUpdateReqVO updateReqVO) {
        wechatTemplateService.updateWechatTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信消息模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> deleteWechatTemplate(@RequestParam("id") Long id) {
        wechatTemplateService.deleteWechatTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信消息模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<WechatTemplateRespVO> getWechatTemplate(@RequestParam("id") Long id) {
        WechatTemplate wechatTemplate = wechatTemplateService.getWechatTemplate(id);
        return success(WechatTemplateConvert.INSTANCE.convert(wechatTemplate));
    }

    @GetMapping("/list")
    @Operation(summary = "获得微信消息模板列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<List<WechatTemplateRespVO>> getWechatTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<WechatTemplate> list = wechatTemplateService.getWechatTemplateList(ids);
        return success(WechatTemplateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信消息模板分页")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<PageResult<WechatTemplateRespVO>> getWechatTemplatePage(@Valid WechatTemplatePageReqVO pageVO) {
        PageResult<WechatTemplate> pageResult = wechatTemplateService.getWechatTemplatePage(pageVO);
        return success(WechatTemplateConvert.INSTANCE.convertPage(pageResult));
    }

}
